var baseurl = '';
var onoff1 = false; //这是导航状态
var tanonoff = true; //弹幕的状态
$(".pothook").click(function() {
	showNav(); //显示或隐藏导航
})
var arrdata = [{
		"headimg": "img/tanmuhead.jpg",
		"message": "好嗨哟"
	},
	{
		"headimg": "img/tanmuhead.jpg",
		"message": "真的好嗨哟"
	},
	{
		"headimg": "img/tanmuhead.jpg",
		"message": "嗨哟"
	},
]

var locaScreen = [120, 160, 200, 240, 280]; //弹幕位置
//初始化
var init = {
	"navdown": "img/navdown2.gif",
	"navup": "img/navup2.gif",
	"timer": 500,
}

function showNav() { //导航栏
	$(".navContent").slideToggle(500);
	if (onoff1) {
		$(".pothook").animate({
			bottom: "60px"
		}, 500)
		$(".pothook")[0].style.background = "url(" + baseurl + "img/navdown2.gif) no-repeat";
		$(".pothook")[0].style.backgroundSize = "100% 80%";

	} else {
		$(".pothook").animate({
			bottom: "-10px"
		}, 500)
		$(".pothook")[0].style.background = "url(" + baseurl + "img/navup2.gif) no-repeat";
		$(".pothook")[0].style.backgroundSize = "100% 80%";
	}
	onoff1 = !onoff1;
}

//var arrdata = [];

var num = -1;
var timer;
var timer1;
initScreen(); //初始屏幕
function initScreen() { //初始化屏幕
	timer = setInterval(function() {
		num++;
		if (num > arrdata.length - 1) {
			num = 0
		}
		$("#wrap").append(createScreenPraise(arrdata[num].headimg, arrdata[num].message, arrdata[num].vote, arrdata[num].state,
			arrdata[num].id));

	}, 6000);
}
//生成弹幕，给弹幕添加praise
function createScreenPraise(heading, message, vote, state, id) {
	var div = document.createElement("div");
	var divContent = document.createElement("div");
	var divPraise = document.createElement("div");
	var color = "";
	var praise = '';
	if (state) { //点过的状态1有颜色
		color = " t-color";
		praise = " t-praise1"
	}
	divPraise.onclick = function() {
		vote++;
		alert(id);
	}
	//消息和头像部分
	divContent.className = "tanmuContent";
	divContent.innerHTML = '<span class="headImg"><img src="' + heading + '" /></span>' + message;
	//放入到最后tanmu元素里
	div.className = "tanmu";
	div.appendChild(divContent);
	createScreen(div) //执行生成弹幕的动画
	return div;
}
$(function() { //点击发送弹幕
	$(".s_sub").click(function() {
		clearInterval(timer);
		clearTimeout(timer1);
		var text = $(".s_txt").val();
		text = text.trim();
		if (!text.length) {
			//alert("请输入想发表的话");//这里是如果发送内容为空；可以加以提示
			$(".send").hide();
			return;
		}
		var img = "http://img3.imgtn.bdimg.com/it/u=634098145,264198475&fm=214&gp=0.jpg";

		$("#wrap").append(createScreenPraise(img, text));
		$(".s_txt")[0].value = "";
		$(".send").hide();
		timer1 = setTimeout(function() {

		}, 2000)
	})
	//==============
	var websocket = null;
	var activityId = localStorage.getItem("activityId");
	//判断当前浏览器是否支持WebSocket
	if ('WebSocket' in window) {
		websocket = new WebSocket("ws://localhost:8080/websocket/111");
	} else {
		alert('Not support websocket')
	}

	//连接发生错误的回调方法
	websocket.onerror = function() {
		console.log("连接失败");
		// setMessageInnerHTML("error");
	};

	//连接成功建立的回调方法
	websocket.onopen = function(event) {
		console.log("连接成功");
		// setMessageInnerHTML("open");
	}

	//接收到消息的回调方法
	websocket.onmessage = function(event) {
		console.log(event);
		setMessageInnerHTML(event.data);
	}

	//连接关闭的回调方法
	websocket.onclose = function() {
		console.log("链接关闭");
		// setMessageInnerHTML("close");
	}

	//监听窗口关闭事件，当窗口关闭时，主动去关闭websocket连接，防止连接还没断开就关闭窗口，server端会抛异常。
	window.onbeforeunload = function() {
		websocket.close();
	}

	//将消息显示在网页上
	function setMessageInnerHTML(jsonx) {
		console.log(jsonx);
		var data = JSON.parse(jsonx);
		var num = $(".acnum").text;
		$(".acnum").text(num + data.length);
		if (data[data.length - 1].activeId == activityId) {
			clearInterval(timer);
			clearTimeout(timer1);
			var text = data[data.length - 1].msg;
			text = text.trim();
			var img = "img/tanmuhead.jpg";
			$("#wrap").append(createScreenPraise(img, text));
			timer1 = setTimeout(function() {

			}, 2000)
		}
	}
});

//给创建的弹幕增加事件
function clickPraise(elem) {
	$(elem).click(function() {
		alert(123)
	})
}

//生成弹幕
var olN = 0;

function createScreen(elem) {
	var _top = 0;
	var _left = $(window).width();
	var _height = $(window).height();

	var lN = Math.floor(5 * Math.random());

	if (olN == lN) {
		lN++;
		if (lN > locaScreen.length - 1) {
			lN = 0;
		}
	}
	olN = lN;
	_top = locaScreen[lN];
	//	console.log(locaScreen.length,_top)
	//初始弹幕的位置
	$(elem).css({
		left: _left,
		top: _top,
		color: getReandomColor()
	});
	//执行动画时间
	var time = 30000;
	//执行动画
	$(elem).animate({
		left: "-" + _left + "px"
	}, time, function() {
		var docum = document.getElementById("wrap");
		docum.removeChild(this);
	});

}
//随机获取颜色值 ；这一个没有加可以在80行“初始弹幕的位置”color：getReandomColor();这样就会生效
function getReandomColor() {
	return '#' + (function(h) {
		return new Array(7 - h.length).join("0") + h
	})((Math.random() * 0x1000000 << 0).toString(16))
}

//导航栏部分的点击事件执行函数
var wid = $(".bzContent")[0].offsetWidth;
var bzc = document.getElementsByClassName('bzContent')[0];

var initScOnff = true;
//这里获取到的是秒，定时器转换毫秒；但是iso获取不audio的时间长度，可以用后台传过来
var times = 106 * 1000; //这里是

//新添加的弹幕，去掉
$(".tanClose")[0].addEventListener('click', adPlay, false);

function adPlay(ev) {
	ev.stopPropagation();
	aud.play();
	aud.pause();
	document.body.removeChild($(".initScreen")[0]);
	setTimeout(function() {
		animationTxt(bzc, times);
	}, 3000)
};

function animationTxt(elem, time) {
	aud.play();
	$(elem).css({
		left: 0
	});
	$(elem).animate({
		left: "-" + wid + "px"
	}, time, "linear", function() {
		setTimeout(function() {
			animationTxt(elem, time);
		}, 6000);

	})
}
$(".c-tanmu")[0].onclick = function() { //弹幕开关
	if (tanonoff) {
		clearInterval(timer);
		$(".navIco-tanmu")[0].style.background = "url(" + baseurl + "img/icotanmuoff.png) no-repeat";
		$(".navIco-tanmu")[0].style.backgroundSize = "100% 100%";
		for (var i = 0; i < $(".tanmu").length; i++) {
			$(".tanmu")[i].style.display = "none";
		}
	} else {
		$(".navIco-tanmu")[0].style.background = "url(" + baseurl + "img/icotanmu.png) no-repeat";
		$(".navIco-tanmu")[0].style.backgroundSize = "100% 100%";
		for (var i = 0; i < $(".tanmu").length; i++) {
			$(".tanmu")[i].style.display = "block";
		}
		initScreen();
	}
	tanonoff = !tanonoff;

}
$(".c-fatan")[0].onclick = function() { //发弹幕
	$(".send").show();
}

function requestFullScreen(element) {
	// 判断各种浏览器，找到正确的方法
	var requestMethod = element.requestFullScreen || //W3C
		element.webkitRequestFullScreen || //Chrome等
		element.mozRequestFullScreen || //FireFox
		element.msRequestFullScreen; //IE11
	if (requestMethod) {
		requestMethod.call(element);
	} else if (typeof window.ActiveXObject !== "undefined") { //for Internet Explorer
		var wscript = new ActiveXObject("WScript.Shell");
		if (wscript !== null) {
			wscript.SendKeys("{F11}");
		}
	}
}
$("body").contextmenu(function() {
	return false;
});


